﻿package core.image.psd
{

    public class PixelHelper extends Object
    {
        private static var count:int = 0;

        public function PixelHelper()
        {
            return;
        }// end function

        public static function ColorFromPSDFile(PixelHelper.as$482:PSDFile, PixelHelper.as$482:int) : uint
        {
            var _loc_3:int = 0;
            switch(PixelHelper.as$482.header.ColorMode)
            {
                case 3:
                {
                    return PixelHelper.ARGBColor(255, PixelHelper.as$482.Data[0][PixelHelper.as$482], PixelHelper.as$482.Data[1][PixelHelper.as$482], PixelHelper.as$482.Data[2][PixelHelper.as$482]);
                }
                case 4:
                {
                    return PixelHelper.CMYKColor(255, PixelHelper.as$482.Data[0][PixelHelper.as$482], PixelHelper.as$482.Data[1][PixelHelper.as$482], PixelHelper.as$482.Data[2][PixelHelper.as$482], PixelHelper.as$482.Data[3][PixelHelper.as$482]);
                }
                case 7:
                case 1:
                case 8:
                {
                    return PixelHelper.ARGBColor(255, PixelHelper.as$482.Data[0][PixelHelper.as$482], PixelHelper.as$482.Data[0][PixelHelper.as$482], PixelHelper.as$482.Data[0][PixelHelper.as$482]);
                }
                case 2:
                {
                    _loc_3 = PixelHelper.as$482.Data[0][PixelHelper.as$482];
                    return PixelHelper.ARGBColor(255, PixelHelper.as$482.IndexData[_loc_3], PixelHelper.as$482.IndexData[_loc_3 + 256], PixelHelper.as$482.IndexData[_loc_3 + 512]);
                }
                case 9:
                {
                    return PixelHelper.LabColor(255, PixelHelper.as$482.Data[0][PixelHelper.as$482], PixelHelper.as$482.Data[1][PixelHelper.as$482], PixelHelper.as$482.Data[2][PixelHelper.as$482]);
                }
                default:
                {
                    break;
                }
            }
            return 0;
        }// end function

        public static function ColorFromLayer(PixelHelper.as$482:PSDFile, PixelHelper.as$482:Layer, PixelHelper.as$482:int) : uint
        {
            var _loc_4:int = 0;
            var _loc_5:* = Boolean(PixelHelper.as$482.SortedChannels[-1]) ? (PixelHelper.as$482.SortedChannels[-1].Data[PixelHelper.as$482]) : (255);
            switch(PixelHelper.as$482.header.ColorMode)
            {
                case 3:
                {
                    return PixelHelper.ARGBColor(_loc_5, PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[1].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[2].Data[PixelHelper.as$482]);
                }
                case 4:
                {
                    return PixelHelper.CMYKColor(_loc_5, PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[1].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[2].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[3].Data[PixelHelper.as$482]);
                }
                case 7:
                case 1:
                case 8:
                {
                    return PixelHelper.ARGBColor(_loc_5, PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482]);
                }
                case 2:
                {
                    _loc_4 = PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482];
                    return PixelHelper.ARGBColor(_loc_5, PixelHelper.as$482.IndexData[_loc_4], PixelHelper.as$482.IndexData[_loc_4 + 256], PixelHelper.as$482.IndexData[_loc_4 + 512]);
                }
                case 9:
                {
                    return PixelHelper.LabColor(_loc_5, PixelHelper.as$482.SortedChannels[0].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[1].Data[PixelHelper.as$482], PixelHelper.as$482.SortedChannels[2].Data[PixelHelper.as$482]);
                }
                default:
                {
                    break;
                }
            }
            return 0;
        }// end function

        public static function ColorFromMask(PixelHelper.as$482:Layer, PixelHelper.as$482:int, PixelHelper.as$482:int) : uint
        {
            var _loc_4:int = 0;
            var _loc_5:uint = 255;
            if ((PixelHelper.as$482.MaskData.Flags & 1) != 0)
            {
                PixelHelper.as$482 = PixelHelper.as$482 - PixelHelper.as$482.MaskData.Dimensions.x;
                PixelHelper.as$482 = PixelHelper.as$482 - PixelHelper.as$482.MaskData.Dimensions.y;
            }
            else
            {
                PixelHelper.as$482 = PixelHelper.as$482 + PixelHelper.as$482.Dimensions.x - PixelHelper.as$482.MaskData.Dimensions.x;
                PixelHelper.as$482 = PixelHelper.as$482 + PixelHelper.as$482.Dimensions.y - PixelHelper.as$482.MaskData.Dimensions.y;
            }
            if (PixelHelper.as$482 >= 0 && PixelHelper.as$482 < PixelHelper.as$482.MaskData.Dimensions.height && PixelHelper.as$482 >= 0 && PixelHelper.as$482 < PixelHelper.as$482.MaskData.Dimensions.width)
            {
                _loc_4 = PixelHelper.as$482 * PixelHelper.as$482.MaskData.Dimensions.width + PixelHelper.as$482;
                if (_loc_4 < PixelHelper.as$482.MaskData.Data.length)
                {
                    _loc_5 = PixelHelper.as$482.MaskData.Data[_loc_4];
                }
                else
                {
                    _loc_5 = 255;
                }
            }
            return _loc_5;
        }// end function

        public static function BlendAlpha(PixelHelper.as$482:uint, PixelHelper.as$482:uint) : uint
        {
            var _loc_3:* = PixelHelper.as$482 << 8 >> 24;
            var _loc_4:* = PixelHelper.as$482 << 16 >> 24;
            var _loc_5:* = PixelHelper.as$482 << 24 >> 24;
            return PixelHelper.as$482 << 24 | _loc_3 << 16 | _loc_4 << 8 | _loc_5;
        }// end function

        public static function ARGBColor(PixelHelper.as$482:uint, PixelHelper.as$482:uint, PixelHelper.as$482:uint, PixelHelper.as$482:uint) : uint
        {
            return PixelHelper.as$482 << 24 | PixelHelper.as$482 << 16 | PixelHelper.as$482 << 8 | PixelHelper.as$482;
        }// end function

        public static function LabColor(PixelHelper.as$482:uint, PixelHelper.as$482:Number, PixelHelper.as$482:Number, PixelHelper.as$482:Number) : uint
        {
            var _loc_5:* = int(PixelHelper.as$482 / 2.56);
            var _loc_6:* = int(PixelHelper.as$482 - 128);
            var _loc_7:* = int(PixelHelper.as$482 - 128);
            var _loc_8:* = (_loc_5 + 16) / 116;
            var _loc_9:* = _loc_6 / 500 + _loc_8;
            var _loc_10:* = _loc_8 - _loc_7 / 200;
            if (Math.pow(_loc_8, 3) > 0.008856)
            {
                _loc_8 = Math.pow(_loc_8, 3);
            }
            else
            {
                _loc_8 = (_loc_8 - 16 / 116) / 7.787;
            }
            if (Math.pow(_loc_9, 3) > 0.008856)
            {
                _loc_9 = Math.pow(_loc_9, 3);
            }
            else
            {
                _loc_9 = (_loc_9 - 16 / 116) / 7.787;
            }
            if (Math.pow(_loc_10, 3) > 0.008856)
            {
                _loc_10 = Math.pow(_loc_10, 3);
            }
            else
            {
                _loc_10 = (_loc_10 - 16 / 116) / 7.787;
            }
            return PixelHelper.XYZColor(PixelHelper.as$482, _loc_9 * 95.047, _loc_8 * 100, _loc_10 * 108.883);
        }// end function

        public static function XYZColor(PixelHelper.as$482:uint, PixelHelper.as$482:Number, PixelHelper.as$482:Number, PixelHelper.as$482:Number) : uint
        {
            var _loc_5:* = PixelHelper.as$482 / 100;
            var _loc_6:* = PixelHelper.as$482 / 100;
            var _loc_7:* = PixelHelper.as$482 / 100;
            var _loc_8:* = _loc_5 * 3.2406 + _loc_6 * -1.5372 + _loc_7 * -0.4986;
            var _loc_9:* = _loc_5 * -0.9689 + _loc_6 * 1.8758 + _loc_7 * 0.0415;
            var _loc_10:* = _loc_5 * 0.0557 + _loc_6 * -0.204 + _loc_7 * 1.057;
            if (_loc_8 > 0.0031308)
            {
                _loc_8 = 1.055 * Math.pow(_loc_8, 1 / 2.4) - 0.055;
            }
            else
            {
                _loc_8 = 12.92 * _loc_8;
            }
            if (_loc_9 > 0.0031308)
            {
                _loc_9 = 1.055 * Math.pow(_loc_9, 1 / 2.4) - 0.055;
            }
            else
            {
                _loc_9 = 12.92 * _loc_9;
            }
            if (_loc_10 > 0.0031308)
            {
                _loc_10 = 1.055 * Math.pow(_loc_10, 1 / 2.4) - 0.055;
            }
            else
            {
                _loc_10 = 12.92 * _loc_10;
            }
            return PixelHelper.ARGBColor(PixelHelper.as$482, PixelHelper.ConstrainToByte(_loc_8 * 256), PixelHelper.ConstrainToByte(_loc_9 * 256), PixelHelper.ConstrainToByte(_loc_10 * 256));
        }// end function

        public static function CMYKColor(PixelHelper.as$482:uint, PixelHelper.as$482:Number, PixelHelper.as$482:Number, PixelHelper.as$482:Number, PixelHelper.as$482:Number) : uint
        {
            var _loc_6:* = 1 - PixelHelper.as$482 / 256;
            var _loc_7:* = PixelHelper.ConstrainToByte((1 - ((1 - PixelHelper.as$482 / 256) * (1 - _loc_6) + _loc_6)) * 255);
            var _loc_8:* = PixelHelper.ConstrainToByte((1 - ((1 - PixelHelper.as$482 / 256) * (1 - _loc_6) + _loc_6)) * 255);
            var _loc_9:* = PixelHelper.ConstrainToByte((1 - ((1 - PixelHelper.as$482 / 256) * (1 - _loc_6) + _loc_6)) * 255);
            return PixelHelper.ARGBColor(PixelHelper.as$482, _loc_7, _loc_8, _loc_9);
        }// end function

        public static function ConstrainToByte(PixelHelper.as$482:Number) : uint
        {
            var _loc_2:* = int(PixelHelper.as$482);
            if (_loc_2 > 255)
            {
                _loc_2 = 255;
            }
            if (_loc_2 < 0)
            {
                _loc_2 = 0;
            }
            return uint(_loc_2);
        }// end function

    }
}
